<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Model\Categories;
use App\Model\Tags;
use App\Model\Articles;

class RefreshCache extends Controller
{   
    /**
     * 刷新与Categories模型相关的缓存
     * @return void
     */
    public static function refreshCategoriesCache()
    {   
        //Category的所有信息
        $categoriesList = Categories::select(['id', 'name', 'description', 'sort', 'status', 'updated_at'])
                ->orderBy('id', 'desc')
                ->get()->toArray();

        //存入Redis
        Redis::set('categories-list-all', json_encode($categoriesList));

        //某个Category的信息
        $categoriesList = Categories::select(['id', 'name', 'description', 'sort', 'status', 'updated_at'])
                ->where('status', 1)->orderBy('id', 'desc')->get()->toArray();

        //存入Redis
        Redis::set('categories-list-active', json_encode($categoriesList));

    }

    /**
     * 刷新与Tags模型相关的缓存
     * @return void
     */
    public static function refreshTagsCache()
    {
        //Tags的所有信息
        $tagsList = Tags::all()->toArray();

        Redis::set('tags-list', json_encode($tagsList));
    }

    /**
     * 刷新与Articles模型相关的缓存
     * @return void
     */
    public static function refreshArticlesCache()
    {   
        //Articles的信息
        $articlesList = Articles::select(['id', 'category_id', 'title', 'author', 'cover', 'is_top', 'click', 'display', 'description', 'updated_at', 'created_at'])
                            ->orderBy('is_top', 'desc')->latest()->get()->toArray();

        Redis::set('articles-list-all', json_encode($articlesList));

        $articlesList = Articles::select(['id', 'category_id', 'title', 'author', 'cover', 'is_top', 'click', 'display', 'description', 'updated_at', 'created_at'])
                            ->where('display', 1)->orderBy('is_top', 'desc')->latest()->get()->toArray();

        Redis::set('articles-list-active', json_encode($articlesList));


    }




}
